package com.kingsoft.dc.khaos.module.spark.model.exchangefile.ddl;

import com.kingsoft.dc.khaos.module.spark.model.StructFieldEntity;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.OutputStream;
import java.util.List;

/**
 * Created by zoujing on 19/3/1.
 */
public class StructFileDDL {
    private String fileName;

    private String fileVersion;

    private String fieldCount;

    private String isfixedlength;

    private List<StructFieldEntity> fielddescription;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileVersion() {
        return fileVersion;
    }

    public void setFileVersion(String fileVersion) {
        this.fileVersion = fileVersion;
    }

    public String getFieldCount() {
        return fieldCount;
    }

    public void setFieldCount(String fieldCount) {
        this.fieldCount = fieldCount;
    }

    public String getIsfixedlength() {
        return isfixedlength;
    }

    public void setIsfixedlength(String isfixedlength) {
        this.isfixedlength = isfixedlength;
    }

    public List<StructFieldEntity> getFielddescription() {
        return fielddescription;
    }

    public void setFielddescription(List<StructFieldEntity> fielddescription) {
        this.fielddescription = fielddescription;
    }

    public void writeXml(String fileName, FileSystem fs) {
        Document document = DocumentHelper.createDocument();
        Element transmitContent = document.addElement("transmit-content");
        Element transmitContentFile = transmitContent.addElement("file");
        Element filenameEl = transmitContentFile.addElement("filename");
        filenameEl.addText(this.getFileName());

        Element fileversionEl = transmitContentFile.addElement("fileversion");
        fileversionEl.addText(this.getFileVersion());

        Element fieldcountEl = transmitContentFile.addElement("fieldcount");
        fieldcountEl.addText(this.getFieldCount());

        Element fieldDescEl = transmitContentFile.addElement("fielddescription");
        for (StructFieldEntity field : this.getFielddescription()) {
            Element fieldNameEl = fieldDescEl.addElement("fieldname");
            fieldNameEl.addText(field.getFieldName());
            Element fieldTypeEl = fieldDescEl.addElement("fieldtype");
            fieldTypeEl.addText(field.getFieldType());
        }
        try {
            OutputStream out = fs.create(new Path(fileName));
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter xmlWriter = new XMLWriter(out, format);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
